package com.bizdk.fastfx.controller;

import com.bizdk.fastfx.fxsupport.FxmlView;
import com.bizdk.fastfx.fxsupport.JavaFxController;
import com.bizdk.fastfx.model.PatientModel;
import com.bizdk.fastfx.service.PatientService;
import com.bizdk.fastfx.utils.FileUtil;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.TilePane;
import javafx.stage.FileChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

@Controller
@FxmlView("/view/AddPatientStage.fxml")
public class AddPatientStageController extends JavaFxController {

    @FXML
    private TextField tf_id;

    @FXML
    private TextField tf_name;

    @FXML
    private ComboBox<String> cb_gender;

    @FXML
    private Button bt_upload;

    @FXML
    private TilePane tp_thumbnail;

    @Autowired
    private PatientService patientService;

    @Autowired
    private MainStageController mainStageController;

    private PatientModel model;

    private FileChooser fileChooser = new FileChooser();

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        getStage().setResizable(false);
        cb_gender.getItems().addAll("男", "女");

        // 创建文件选择器
        fileChooser.setTitle("选择图片");
        fileChooser.getExtensionFilters().addAll(
                new FileChooser.ExtensionFilter("所有图片类型", "*.jpg", "*.png", "*.jpeg"),
                new FileChooser.ExtensionFilter("JPG格式", "*.jpg"),
                new FileChooser.ExtensionFilter("PNG格式", "*.png"),
                new FileChooser.ExtensionFilter("JPEG格式", "*.jpeg")
        );
    }

    public void handlerSave(ActionEvent event) {
        event.consume();
        if (null == model) {
            model = new PatientModel();
        }
        model.setName(tf_name.getText());
        model.setGender(cb_gender.getValue());
        if (StringUtils.isEmpty(model.getId())) {
            patientService.addModel(model);
        } else {
            patientService.updateModel(model);
        }
        mainStageController.getPartientList();
        getStage().close();
    }

    public void handlerReset(ActionEvent event) {
        event.consume();
        reset();
    }

    public void handlerUpload(ActionEvent event){
        event.consume();
        List<File> fileList = fileChooser.showOpenMultipleDialog(getStage()); // 用于选择多张图片
        if (fileList != null) {
            ObservableList<ImageView> imageViews = model.getImageViews();
            for (File file : fileList) {
                try {
                    String fileName = file.getName();
                    String fileNameNow = fileName.substring(fileName.lastIndexOf(File.separatorChar) + 1);
                    String path = "images" + File.separatorChar + fileNameNow;
                    File destFile = new File(path);
                    FileUtil.copyFile(file, destFile);
                    Image image = new Image(new FileInputStream(destFile));
                    ImageView imageView = new ImageView(image);
                    imageView.setUserData(path);
                    imageView.setFitWidth(100);
                    imageView.setFitHeight(100);
                    imageViews.add(imageView); // 将ImageView加入到列表中
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (!imageViews.isEmpty()) {
                tp_thumbnail.getChildren().clear();
                tp_thumbnail.getChildren().addAll(imageViews);
            }
        }
    }


    public void show(PatientModel model) {
        this.model = model;
        Stage stage = getStage();
        if (null == stage) {
            stage = new Stage();
            stage.initOwner(mainStageController.getStage()); //设置两个窗口关联，s2窗口的拥有者为s1
            stage.initModality(Modality.WINDOW_MODAL); //s2设置模块化，对s1窗口起作用
            setStage(stage);
        }
        Scene scene = getScene();
        if (null == scene) {
            scene = new Scene(getRoot());
            setScene(scene);
        } else {
            scene.setRoot(getRoot());
        }
        stage.setScene(scene);
        stage.centerOnScreen();
        stage.show();

        if (null == model.getId()) {
            getStage().setTitle("新增病人");
        } else {
            getStage().setTitle("编辑病人");
        }

        reset();
    }

    private void reset() {
        tf_id.setText(model.getId());
        tf_name.setText(model.getName());
        cb_gender.getSelectionModel().select(model.getGender());
        tp_thumbnail.getChildren().clear();
        ObservableList<ImageView> imageViews = model.getImageViews();
        if (null != imageViews  && imageViews.size() > 0) {
            tp_thumbnail.getChildren().addAll(imageViews);
        }
    }
}
